package com.tanhua.sso.controller;

import com.tanhua.common.pojo.User;
import com.tanhua.common.vo.PicUploadResult;
import com.tanhua.sso.service.UserInfoService;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@Slf4j
@RequestMapping("user")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 录入用户信息
     */
    @PostMapping("loginReginfo")
    public ResponseEntity<Object> loginRegInfo(@RequestBody Map<String, String> param, @RequestHeader("Authorization") String token) {
        try {
            Boolean flag = userInfoService.loginReginfo(param, token);
            if (flag) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("录入用户信息异常!");
            return ResponseEntity.status(500).body(null);
        }
        log.error("录入用户信息失败!");
        return ResponseEntity.status(500).body(null);
    }

    @PostMapping("loginReginfo/head")
    public ResponseEntity<Object> uploadLogo(@RequestParam("headPhoto") MultipartFile file, @RequestHeader("Authorization") String token) {
        try {
            PicUploadResult picUploadResult = userInfoService.uploadLogo(file, token);
            if (picUploadResult == null) {
                return ResponseEntity.status(500).body(ErrorResult.builder().errCode("10003").errMessage("上传图像失败!Controller!").build());
            }
        } catch (Exception e) {
            return ResponseEntity.status(500).body(ErrorResult.builder().errCode("10004").errMessage("上传图像异常!Controller!").build());
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 提供远程token校验的功能
     */
    @GetMapping("{token}")
    public User QueryTokenCheck(@PathVariable("token") String token) {
        return userInfoService.checkToken(token);
    }

}
